﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LongSCADA_WPF.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LongSCADA_WPF.ViewModels
{
    public partial class LogViewModel:ObservableObject
    {
        [ObservableProperty]
        ObservableCollection<FileInfo> _logFile = new();

        [ObservableProperty]
        ObservableCollection<LogModel> _logEntries = new();

        [ObservableProperty]
        FileInfo _selectedLogFile;
        partial void OnSelectedLogFileChanged(FileInfo value)
        {
            //当 _selectedLogFile 发生变化时，重新加载日志文件
            LogEntries.Clear();
            if (value != null)
            {
                try
                {
                    string[] lines = File.ReadAllLines(value.FullName);

                    foreach (string line in lines)
                    {
                        LogModel? logModel = LogModel.Parse(line);
                        if (logModel != null)
                        {
                            LogEntries.Add(logModel);
                        }
                    }
                }
                catch (Exception ex)
                {

                   Debug.WriteLine(ex.Message);
                }
            }
        }

        [RelayCommand]
        void OpenLogFolder()
        {
            //显示于 ListView

            try
            {
                LogFile.Clear();

                string logsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
                if (Directory.Exists(logsPath))
                {
                    //遍历一周的日志文件
                    DateTime currentTime = DateTime.Now;
                    DateTime oneWeekAgo = currentTime.AddDays(-30);

                    //获取日志文件夹下所有日期文件夹
                    IEnumerable<DirectoryInfo> dateFolders = Directory.GetDirectories(logsPath)
                        .Where(dir => DateTime.TryParse(Path.GetFileName(dir), out DateTime logTime))
                        .Select(dir => new DirectoryInfo(dir));

                    IEnumerable<DirectoryInfo> recentFolders = dateFolders.Where(dir =>
                    {
                        if (DateTime.TryParse(dir.Name, out DateTime logTime))
                        {
                            return logTime >= oneWeekAgo && logTime <= currentTime;
                        }
                        return false;
                    });

                    //获取满足日期的日志文件
                    IOrderedEnumerable<FileInfo> logFiles = recentFolders.SelectMany(dir =>
                        dir.GetFiles("*.log", SearchOption.AllDirectories))
                        .OrderByDescending(file => file.LastWriteTime);

                    LogFile = new ObservableCollection<FileInfo>(logFiles);
                }
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }
        }
    }
}
